package jsu.xnm.evelyn.util;

/**
 * @author xnm
 * @version 1.0
 */

/**
 * 自定义泛型类（统一响应格式）
 * @param <T>
 */
public class Result<T>{
    private String code; //状态码
    private String msg;  //状态说明
    private T data;     //返回数据(使用泛型)

    //1.success-返回成功需要的Result对象
    public static Result success(){
        Result result = new Result();
        result.setCode("200");
        result.setMsg("success");
        return result;
    }

    //2.success-返回成功需要的Result对象-携带数据
    //注意：如果要在静态方法上使用泛型需要再static 后指明泛型标注 static <T>
    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>(data);
        result.setCode("200");
        result.setMsg("success");
        return result;
    }

    //3.fail-返回失败需要的Result对象
    public static Result error(String code, String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    //4.fail-返回失败需要的Result对象-返回数据
    public static <T> Result<T> error(String code,String msg,T data){
        Result<T> result = new Result<>(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public Result() {
    }

    public Result(T data) {//指定返回data
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
